const regeneratorRuntime = require('../../../utils/runtime.js')
const $ = require('../../../utils/util.js')
const app = getApp()
var Store = app.Store

Page({
  data: {
    user_id: 0, // 用户id
    cabinet_name: '', // 电表箱名称
    cabinet_id: 0, // 电表箱id
    lock_code: '', // 蓝牙锁设备id
    lock_name: '', // 蓝牙锁名称
    dept_id: 0, // 设备id
    company_id: 0, // 企业id
    install_area: '', // 安装区域
  },
  onLoad: async function (options) {
     this.setData({
       dept_id: options.dept_id || 0,
       company_id: options.company_id || 0, 
       lock_code: Store.getState().main.lock_code
     })
  }, 
  onShow: function () {
    if (Store.getState().main.isFromChooseCabinet){

      this.setData({
        cabinet_name: Store.getState().main.cabinet_name,
        cabinet_id: Store.getState().main.cabinet_id,
      })

      Store.dispatch({
        type: 'UPDATE_IS_FROM_CHOOSECABINET',
        isFromChooseCabinet: !!0
      })

      Store.dispatch({
        type: 'UPDATE_CABINET_NAME',
        cabinet_name: ''
      })

      Store.dispatch({
        type: 'UPDATE_CABINET_ID',
        cabinet_id: 0
      })
    }
  },
  //input 输入
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.name]: e.detail
    })
  },
  // 去选择选择当前公司下的电表柜
  toChooseCabinet(){
    $.linkurl(`/pages/lockMange/chooseCabinet/chooseCabinet?company_id=${this.data.company_id}`)
  },
  // 蓝牙锁入库
  _lockadd(){
    if ($.isNull(this.data.lock_name)){
      $.toast('请输入锁名称')
    } else if ($.isNull(this.data.cabinet_id)) {
      $.toast('请输入电表柜名称')
    } 
    // else if ($.isNull(this.data.install_area)) {
    //   $.toast('请输入位置信息')
    // } 
    else {
      return new Promise((resolve, reject) => {
        app.$http.post('/lock/lockadd', {
          user_id: wx.getStorageSync('user_id') || 0,
          cabinet_id: this.data.cabinet_id,
          lock_code: this.data.lock_code,
          lock_name: this.data.lock_name,
          dept_id: this.data.dept_id,
          company_id: this.data.company_id,
          install_area: this.data.install_area,
        }).then(res => {
          if(res && res.code == 0 ) {
            console.log('----------',res)
            $.toast('入库成功')
            $.linkurl('/pages/lockMange/lockIndex/lockIndex')
            resolve(res)
          }
        })
      })
    }
  }
})